package Transaksi;

import Barang.Barang;
import Kasir.Kasir;
import Koneksi.Koneksi;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Astri
 */
public class TransaksiKontrol {
    
    Connection konek;
    private static TransaksiKontrol transaksiKontrol;
    private Kasir kasir;
    
    public TransaksiKontrol(Connection konek) {
        this.konek = konek;
    }
    
    public static TransaksiKontrol getKoneksiKontrol() {
        Koneksi k = new Koneksi();
        if (transaksiKontrol == null) {
            transaksiKontrol = new TransaksiKontrol(k.getConnection());
        }
        return transaksiKontrol;
    }
    
    TransaksiKontrol() {
    }
    
    public Barang cariBarang(Barang barang) throws SQLException {
        CallableStatement cl = null;
        String sql = "call cariBarang(?,?,?,?,?,?)";
        cl = konek.prepareCall(sql);
        cl.setString(1, barang.getKodeBarang());
        cl.registerOutParameter(2, java.sql.Types.VARCHAR);
        cl.registerOutParameter(3, java.sql.Types.VARCHAR);
        cl.registerOutParameter(4, java.sql.Types.DOUBLE);
        cl.registerOutParameter(5, java.sql.Types.DOUBLE);
        cl.registerOutParameter(6, java.sql.Types.DOUBLE);
        cl.execute();
        
        String nama = cl.getString(2);
        String jenis = cl.getString(3);
        Double hargajual = cl.getDouble(4);
        Double hargabeli = cl.getDouble(5);
        Double jumlahstockawal = cl.getDouble(6);
        cl.execute();
        
        barang.setNamaBarang(nama);
        barang.setJenisBarang(jenis);
        barang.setHargaJual(hargajual);
        barang.setHargaBeli(hargabeli);
        barang.setJumlahStockAwal(jumlahstockawal);
        return barang;
    }
    
    public double hitungTotal(int item, double harga) {
        return (item * harga) - hitungDiskon(item, harga);
    }
    
    public double hitungDiskon(int item, double harga) {
        return item * (0.10 * harga);
    }
    
    public double hitungSisa(double masuk, double keluar) {
        return masuk - keluar;
    }
    
    public void tambahTransaksi(Transaksi transaksi) throws SQLException {
        PreparedStatement prepare = null;
        java.sql.Date Tgl_Transaksi = transaksi.getTgl_Transaksi();
        String Nomor_Nota = transaksi.getNomor_Nota();
        String Id_Kasir = transaksi.getId_Kasir();
        Double Total_Pembayaran = transaksi.getTotal_Pembayaran();
        String sql = "call tambahTransaksi(?,?,?,?)";
        prepare = konek.prepareStatement(sql);
        prepare.setString(1, Nomor_Nota);
        prepare.setString(2, Id_Kasir);
        prepare.setDouble(3, Total_Pembayaran);
        prepare.setDate(4, Tgl_Transaksi);
        prepare.executeUpdate();
    }    
    
    
    public List<Transaksi> getAllTransaksi() throws SQLException {
        PreparedStatement prepare = null;
        ResultSet rs = null;
        String sql = "select Nomor_Nota,Total_Pembayaran from TransaksiPenjualan order by Nomor_Nota";
        prepare = konek.prepareStatement(sql);
        rs = prepare.executeQuery();
        List<Transaksi> trans = new ArrayList<Transaksi>();
        while (rs.next()) {
            Transaksi transaksi = new Transaksi();
            String noNota = rs.getString("Nomor_Nota");
            Double total = Double.parseDouble(rs.getString("Total_Pembayaran"));
            transaksi.setNomor_Nota(noNota);
            transaksi.setTotal_Pembayaran(total);
            trans.add(transaksi);
        }
        konek.commit();
        return trans;
        
    }
 
}
